package leetcode.每日一题;

import leetcode.util.CommonUtils;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author ：zsy
 * @date ：Created 2022/11/15 11:15 PM
 * @description：
 */
public class 卡车上的最大单元数 {
    @Test
    public void test() {
        CommonUtils.arrTransfer("[[1,3],[2,2],[3,1]]");
        int[][] nums = {{1, 3}, {2, 2}, {3, 1}};
        System.out.println(new Solution().maximumUnits(nums, 5));
    }

    class Solution {
        public int maximumUnits(int[][] boxTypes, int truckSize) {
            Arrays.sort(boxTypes, (a, b) -> b[1] - a[1]);
            int ans = 0;
            for (int[] boxType : boxTypes) {
                if (boxType[0] < truckSize) {
                    ans += boxType[0] * boxType[1];
                } else {
                    ans += truckSize * boxType[1];
                    return ans;
                }
                truckSize -= boxType[0];
            }
            return ans;
        }
    }
}
